package com.metka.volidata.exception;

import ch.qos.logback.core.net.SyslogOutputStream;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: Metka499
 * @Description:
 * @CreateTime: 2020/7/6 15:02
 */

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value =MethodArgumentNotValidException.class)
    public String exceptionHandler(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        String errorMesssage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMesssage += fieldError.getDefaultMessage() + "!";
        }
        return errorMesssage;
    }

    @ExceptionHandler(value = ValidIllegalArgumentException.class)
    public String validIllegalArgumentException(ValidIllegalArgumentException e){
        return e.getMessage();
    }

    @ExceptionHandler(value = Exception.class)
    public String validataException(Exception e){
        System.out.println(e);
        return e.getMessage();
    }
}
